@extends('admin.layout.layout', ['row' => @$page])

@section('contain')


    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/{{ request()->path() }}/../" method="post" onsubmit="return checkVal(this)" class="distributor-form">
            @csrf
            <h3 class="section_sub_title">{{ ts('baseinformation') }}</h3>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('ZH') }}</div>
                <div class="form-input">
                    <div class="input-group custom-control-inline">
                        <input type="text" name="unique_id" value="{{ $accountId ?: old('unique_id') }}" class="form-control">
                    </div>
                    <div id="did1" class="alert alert-danger alert-dismissable">{{ ts('DCL1') }}</div>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('password') }}</div>
                <div class="form-input">
                    <div class="input-group custom-control-inline">
                        <input type="text" name="password" value="{{ $data->password ?: old('password') }}" class="form-control pasword-input">
                        <div class="input-group-prepend">
                            <button type="button" class="btn btn-primary btn-rpwd" onclick="rpwd()">{{ ts('SJSC') }}</button>
                        </div>
                    </div>
                </div>
                <div class="text-secondary ml-3 mt-1">{{ ts('MMCDBDXY') }}</div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">名称</div>
                <div class="form-input">
                    <input type="text" name="abbr" value="{{ $data->abbr ?: old('abbr') }}" class="form-control" >
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('PHONENUMBER') }}</div>
                <div class="form-input">
                    <div class=" input-group custom-control-inline">
                        <div class="input-group-prepend">
                            <select name="extension" class="form-control">
                                @foreach(\App\Models\PhoneExtension::get(['id', 'phone_ext']) as $v)
                                    <option value="{{ $v->phone_ext }}" {{ ($data->extension == $v->phone_ext || old('extension')==$v->phone_ext) ? 'selected' : '' }}>{{ $v->phone_ext }}</option>
                                @endforeach
                            </select>
                        </div>
                        <input type="number" name="phone_number" value="{{ $data->phone_number ?: old('phone_number') }}" class="form-control" >
                    </div>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label">{{ ts('EMAIL') }}</div>
                <div class="form-input">
                    <input type="email" name="email_address" value="{{ $data->email_address ?: old('email_address') }}" class="form-control">
                    {{--<div id="did3" class="alert alert-danger alert-dismissable">邮箱格式不符合</div>--}}
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('status') }}</div>
                <div class="form-input">
                    <select name="status" class="form-control">
                        <option value="1" {{ ($data->status == 1 || old('status')==1) ? 'selected' : '' }}>{{ ts('active') }}</option>
                        <option value="0" {{ ($data->status != 1 && old('status')!=1) ? '' : 'selected' }}>{{ ts('deactive') }}</option>
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">角色</div>
                <div class="form-input">
                    <select name="role_id" class="form-control role_id">
                        <option value="0">请选择角色</option>
                        @if (!empty($roles))
                            @foreach ($roles as $item)
                                @if (mb_strpos($item->simple_chinese_name, '省代') === false)
                                <option value="{{ $item->id }}">{{ $item->simple_chinese_name }}</option>
                                @endif
                            @endforeach
                        @endif
                    </select>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label">管理省代</div>
                <div class="form-input" style="width: 100%;flex: none;max-width: 1000px;">
                    <input type="checkbox" class="batch_select" value="">全选
                    @if (!empty($distributions))
                        @foreach ($distributions as $item)
                            <input type="checkbox" name="distributions_id[]" class="distributions_id" value="{{ $item->id }}">{{ $item->abbr }}
                        @endforeach
                    @endif
                </div>
            </div>

            <h3 class="section_sub_title">{{ ts('contactInformation') }}</h3>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('contactperson') }}</div>
                <div class="form-input">
                    <input type="text" name="first_name" value="{{ $data->first_name ?: old('first_name') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('sjh') }}</div>
                <div class="form-input">
                    <input type="number" name="cell_phone" value="{{ $data->cell_phone ?: old('cell_phone') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('WECHATID') }}</div>
                <div class="form-input">
                    <input type="text" name="wechat_id" value="{{ $data->wechat_id ?: old('wechat_id') }}" class="form-control" >
                </div>
            </div>
            <h3 class="section_sub_title">{{ ts('explain') }}</h3>
            <div class="mt-3" style="width: 650px;max-width: 100%;padding-left: 2rem;">
                <textarea name="note" id="" class="form-control">{{ @$data->note ?: old('note') }}</textarea>
            </div>
            <div class="mt-4">
                <button class="btn btn-primary">{{ ts('submit') }}</button>
            </div>
        </form>

    </div>


@endsection

@section('script')
    <script>
        $(".batch_select").on("change", function () {
           let value = $(this).prop('checked');
            $(".distributions_id").each(function () {
                $(this).prop('checked', value);
            });
        });
        $(".role_id").select2();
        $('body').on('change blur input propertychange', 'input[name="email_address"]', function () {
            var test = /^\w{1,}@\S{1,}\.\w{1,}$/;
            var val = $(this).val();
            if (!!(val) && !test.test(val)) {
                console.log(val, !val);
                // $('#did3').addClass('active');
            } else {
                console.log('active', !!(val) && !test.test(val), !val);
                // $('#did3').removeClass('active');
            }
        });

        $('form.distributor-form').on('submit', function () {
            var form = formObjV2('form.distributor-form');
            var msg = '';
            if (form.unique_id === '' || typeof form.unique_id === 'undefined') {
                msg = '账号不符合规则'
                alert(msg);
                $(this).find('input[name="unique_id"]').focus();
            }
            var password_val = $(this).find('input[name="password"]').val();
            if (form.email_address && !myTestV1('email_address', form.email_address)) {
                if (!myTestV1('email', email_address_val)) {
                    msg = '邮箱 格式不符合规范！';
                    alert(msg);
                    $(this).find('input[name="email_address"]').focus();
                }
            }
            if (password_val.length < 6 && !msg) {
                msg = '密码设置不得低于6位';
                alert(msg);
                $(this).find('input[name="password"]').focus();
            }

            if (!form.phone_number) {
                msg = '联系电话 不能为空！';
            }

            if (!form.first_name) {
                msg = '联系人 不能为空！';
            }

            if (msg) {
                return false;
            }  else {
                return true;
            }
        })

        function rpwd() {
            $('.btn-rpwd').attr('disabled', true);
            var form = {
                t: 'makePassword',
            };
            var url = '/admin/ajax/users';
            request.post(url, form)
                .then(function (d) {
                    console.log(d);
                    $('.pasword-input[name="password"]').val(d.data.pwd)

                    setTimeout(function () {
                        $('.btn-rpwd').attr('disabled', false)
                    }, 1000)
                })
                .catch(function (d) {

                    setTimeout(function () {
                        $('.btn-rpwd').attr('disabled', false)
                    }, 1000)
                })
        }


    </script>
@endsection